package com.quectel.emp.controller.household;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.quectel.constant.core.household.HouseholdConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.dto.HouseholdRelationsDto;
import com.quectel.core.module.household.service.HouseholdFamilyService;
import com.quectel.core.module.household.service.HouseholdRoomService;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.household.service.HouseholdSocialRelationsService;
import com.quectel.core.module.village.service.VillageBuildingService;
import com.quectel.core.module.village.service.VillageFloorService;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.emp.controller.BaseController;
import com.quectel.emp.util.SessionHolder;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-02 13:11:28
 */
@RestController
@RequestMapping("household")
@Tag(name = "HouseholdController", description = "住户相关api")
public class HouseholdController extends BaseController {

    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private HouseholdRoomService householdRoomService;
    @DubboReference
    private VillageService villageService;
    @DubboReference
    private VillageBuildingService villageBuildingService;
    @DubboReference
    private VillageFloorService villageFloorService;
    @DubboReference
    private VillageRoomService villageRoomService;
    @DubboReference
    private HouseholdFamilyService householdFamilyService;
    @DubboReference
    private HouseholdSocialRelationsService householdSocialRelationsService;


    /**
     * 查询住户家庭或者社会关系列表
     */
    @GetMapping("queryHouseholdRelation")
    @Operation(summary = "查询住户家庭或者社会关系列表")
    public Response<HouseholdRelationsDto> queryHouseholdRelation(
            @Parameter(description = "住户ID") @RequestParam Long householdId,
            @Parameter(description = "全部0家庭1社会2") @RequestParam String queryFlag
    ) {

        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(householdId));
        if (householdService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        HouseholdRelationsDto householdRelationsDto = new HouseholdRelationsDto();
        householdRelationsDto.setHouseholdId(householdId);

        if (HouseholdConstants.RELATION_RANGE_FLAG_ALL.equals(queryFlag) || HouseholdConstants.RELATION_RANGE_FLAG_FAMILY.equals(queryFlag)) {
            //家庭关系
            householdRelationsDto.setHouseholdFamilyList(householdFamilyService.queryByHouseholdId(householdId));
        }

        if (HouseholdConstants.RELATION_RANGE_FLAG_ALL.equals(queryFlag) || HouseholdConstants.RELATION_RANGE_FLAG_SOCIAL.equals(queryFlag)) {
            //社会关系
            householdRelationsDto.setSocialRelationsList(householdSocialRelationsService.queryByHouseholdId(householdId));
        }


        return Response.<HouseholdRelationsDto>ok().wrap(householdRelationsDto);
    }


    /**
     * 特殊人群分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("specialList")
    @Operation(summary = "特殊人群分页列表")
    public Response<Page<HouseholdDto>> specialList(
            @Parameter(description = "人员特征,用逗号隔开,字典值HouseholdTag,示例't1,t2,t3'") @RequestParam(required = false) String searchKeysString,
            @Parameter(description = "住户姓名") @RequestParam(required = false) String householdName,
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        if (request.isNotBlank(HouseholdConstants.SEARCH_KEYS_STRING)) {
            request.put(HouseholdConstants.TAG_KEYS, ListUtil.toList(request.getString(HouseholdConstants.SEARCH_KEYS_STRING).split(",")));
        }

        //姓名模糊查询
        if (StringUtils.isNotBlank(householdName)) {
            request.put(LambdaUtil.getFieldName(HouseholdDto::getName), householdName);
        }

        //添加租户和小区筛选
        request.put(LambdaUtil.getFieldName(HouseholdDto::getTenantId), SessionHolder.getEmp().getTenantId());
        request.put(LambdaUtil.getFieldName(HouseholdDto::getVillageId), SessionHolder.getEmp().getVillageId());
        //添加重点人员筛选
        request.put(HouseholdConstants.FOCUS_GROUPS_FLAG, SystemConstants.YES);

        int total = householdService.queryTotal(request.getParams());

        List<HouseholdDto> list = null;
        if (total > 0) {
            list = householdService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<HouseholdDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<HouseholdDto>>ok().wrap(page);
    }


    /**
     * 根据id查询住户单条记录
     */
    @GetMapping("infoForEdit/{id}")
    @Operation(summary = "根据id查询住户单条记录，用户编辑反显")
    public Response<HouseholdDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdDto householdDto = householdService.selectById(id);
        householdDto.setHouseholdRoomDtoList(householdRoomService.selectByHouseholdId(householdDto.getId()));
        return Response.<HouseholdDto>ok().wrap(householdDto);
    }

    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @Operation(summary = "根据id查询单条住户记录")
    public Response<HouseholdDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdDto householdDto = householdService.selectById(id);
        if (householdDto != null) {
            householdDto.setName(DesensitizedUtil.chineseName(householdDto.getName()));
            householdDto.setMobile(DesensitizedUtil.mobilePhone(householdDto.getMobile()));
            householdDto.setCardNo(DesensitizedUtil.idCardNum(householdDto.getCardNo(), 2, 2));
        }
        householdDto.setHouseholdRoomDtoList(householdRoomService.selectByHouseholdId(householdDto.getId()));

        return Response.<HouseholdDto>ok().wrap(householdDto);
    }

}
